package com.project.controller;

import com.framework.core.result.APIListResponse;
import com.framework.core.result.APIObjResponse;
import com.framework.core.result.BasePage;
import com.project.base.BaseController;
import com.project.biz.CmsOrderBiz;
import com.project.common.model.UserInfo;
import com.project.entity.CmsOrder;
import com.project.pojo.dto.*;
import com.project.pojo.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 商品订单
 * @Author: zhuchi
 * @CreateDate: 2020/10/16 23:00
 * @Version: 1.0
 */
@Slf4j
@Api(value = "CmsOrderController", description = "商品订单")
@RestController
@RequestMapping("/order")
public class CmsOrderController extends BaseController {

    /**
     * 商品订单业务层接口类
     */
    @Autowired
    private CmsOrderBiz orderBiz;

    /**
     * 商品下单
     * @param createOrderModel
     * @return
     */
    @ApiOperation(value = "商品下单")
    @PostMapping(value = "/createOrder")
    public APIObjResponse<CreateOrderDTO> createOrder(@Validated @RequestBody CreateOrderModel createOrderModel){
        UserInfo userInfo = super.getLoginUser();
        log.info("用户[{}]购买商品下单入参:{}", userInfo.getBaseUserId(), createOrderModel);
        CreateOrderDTO createOrderDTO = this.orderBiz.createOrder(createOrderModel, userInfo.getBaseUserId());

        return APIObjResponse.success("操作成功", createOrderDTO);
    }

    /**
     * 后台-获取订单列表
     * @param getOrderListModel
     * @return
     */
    @ApiOperation(value = "后台-获取订单列表")
    @PostMapping(value = "/getOrderList")
    public APIObjResponse<BasePage<ProductOrderListDTO>> getOrderList(@RequestBody GetOrderListModel getOrderListModel){
        BasePage<ProductOrderListDTO> basePage = this.orderBiz.getOrderList(getOrderListModel);

        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 获取我的订单列表
     * @param getOrderListModel
     * @return
     */
    @ApiOperation(value = "获取我的订单列表")
    @PostMapping(value = "/getMyProductOrder")
    public APIObjResponse<BasePage<ProductOrderListDTO>> getMyProductOrder(@RequestBody GetOrderListModel getOrderListModel){
        UserInfo userInfo = super.getLoginUser();
        getOrderListModel.setUserId(userInfo.getBaseUserId());
        BasePage<ProductOrderListDTO> basePage = this.orderBiz.getOrderList(getOrderListModel);

        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 获取订单详情-前后台通用
     * @param orderId
     * @return
     */
    @ApiOperation(value = "获取订单详情-前后台通用")
    @GetMapping(value = "/getProductOrderDetail")
    public APIObjResponse<ProductOrderDetailDTO> getProductOrderDetail(@RequestParam("orderId") Long orderId){
        ProductOrderDetailDTO productOrderDetailDTO = this.orderBiz.getProductOrderDetail(orderId);

        return APIObjResponse.success("查询成功", productOrderDetailDTO);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @ApiOperation(value = "取消订单")
    @GetMapping(value = "/cancelOrder")
    public APIObjResponse<Integer> cancelOrder(@RequestParam("orderId") Long orderId){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateCancelOrder(orderId, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @ApiOperation(value = "删除订单")
    @GetMapping(value = "/delOrder")
    public APIObjResponse<Integer> delOrder(@RequestParam("orderId") Long orderId){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateDelOrder(orderId, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 订单支付
     * @param orderId
     * @return
     */
    @ApiOperation(value = "订单支付")
    @GetMapping(value = "/orderPay")
    public APIObjResponse<OrderPayDTO> orderPay(@RequestParam("orderId") Long orderId){
        UserInfo userInfo = super.getLoginUser();
        OrderPayDTO result = this.orderBiz.updateOrderPay(orderId, userInfo.getBaseUserId(), super.getRealIP());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 订单支付JSAPI
     * @param orderPayJsapiModel
     * @return
     */
    @ApiOperation(value = "订单支付JSAPI")
    @PostMapping(value = "/orderPayJsapi")
    public APIObjResponse<OrderPayJsapiDTO> orderPayJsapi(@RequestBody OrderPayJsapiModel orderPayJsapiModel){
        UserInfo userInfo = super.getLoginUser();
        log.info("订单支付JSAPI入参:{}", orderPayJsapiModel);
        OrderPayJsapiDTO orderPayJsapiDTO = this.orderBiz.updateOrderPayJsapi(orderPayJsapiModel, userInfo.getBaseUserId(), super.getRealIP());
        return APIObjResponse.success("操作成功", orderPayJsapiDTO);
    }

    /**
     * 查询订单状态
     * @param orderId
     * @return
     */
    @ApiOperation(value = "查询订单状态}")
    @GetMapping(value = "/selectOrderPayState")
    public APIObjResponse<Integer> selectOrderPayState(@RequestParam("orderId") Long orderId){
        CmsOrder order = this.orderBiz.getById(orderId);
        if (null != order){
            return APIObjResponse.success("查询成功", order.getState());
        }else {
            return APIObjResponse.error("订单不存在");
        }
    }

    /**
     * 商品订单发货
     * @param productOrderIssueModel
     * @return
     */
    @ApiOperation(value = "商品订单发货")
    @PostMapping(value = "/productOrderIssue")
    public APIObjResponse<Integer> productOrderIssue(@Validated @RequestBody ProductOrderIssueModel productOrderIssueModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateProductOrderIssue(productOrderIssueModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 查看物流
     * @param orderId
     * @return
     */
    @ApiOperation(value = "查看物流")
    @GetMapping(value = "/getProductOrderLogistics")
    public APIListResponse<ProductOrderLogisticsDTO> getProductOrderLogistics(@RequestParam("orderId") Long orderId){
        List<ProductOrderLogisticsDTO> productOrderLogisticsDTOS = this.orderBiz.getProductOrderLogistics(orderId);
        return APIListResponse.success("查询成功", productOrderLogisticsDTOS);
    }

    /**
     * 确认收货
     * @param productOrderConfirmReceiptModel
     * @return
     */@ApiOperation(value = "确认收货")
    @PostMapping(value = "/productOrderConfirmReceipt")
    public APIObjResponse<Integer> productOrderConfirmReceipt(@Validated @RequestBody ProductOrderConfirmReceiptModel productOrderConfirmReceiptModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateProductOrderConfirmReceipt(productOrderConfirmReceiptModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 评价
     * @param productOrderEvaluateModel
     * @return
     */@ApiOperation(value = "评价")
    @PostMapping(value = "/productOrderEvaluate")
    public APIObjResponse<Integer> productOrderEvaluate(@Validated @RequestBody ProductOrderEvaluateModel productOrderEvaluateModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateProductOrderEvaluate(productOrderEvaluateModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 申请退款
     * @param productOrderApplyRefundModel
     * @return
     */
    @ApiOperation(value = "申请退款")
    @PostMapping(value = "/productOrderApplyRefund")
    public APIObjResponse<Integer> productOrderApplyRefund(@Validated @RequestBody ProductOrderApplyRefundModel productOrderApplyRefundModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateOrderApplyRefund(productOrderApplyRefundModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 后台-同意退款
     * @param agreeProductOrderRefundModel
     * @return
     */
    @ApiOperation(value = "后台-同意退款")
    @PostMapping(value = "/agreeProductOrderRefund")
    public APIObjResponse<Integer> agreeProductOrderRefund(@Validated @RequestBody AgreeProductOrderRefundModel agreeProductOrderRefundModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateAgreeProductOrderRefund(agreeProductOrderRefundModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 后台-拒绝退款
     * @param orderId
     * @return
     */
    @ApiOperation(value = "后台-拒绝退款")
    @GetMapping(value = "/refuseProductOrderRefund")
    public APIObjResponse<Integer> refuseProductOrderRefund(@RequestParam("orderId") Long orderId){
        UserInfo userInfo = super.getLoginUser();
        int result = this.orderBiz.updateAgreeProductOrderRefund(orderId, userInfo.getBaseUserId());
        return APIObjResponse.success("查询成功", result);
    }

    /**
     * 获取用户商品订单、回收订单各状态数量
     * @return
     */
    @ApiOperation(value = "获取用户商品订单、回收订单各状态数量")
    @GetMapping(value = "/getUserCenterOrderNum")
    public APIObjResponse<GetOrderNumDTO> getUserCenterOrderNum(){
        UserInfo userInfo = super.getLoginUser();
        GetOrderNumDTO getOrderNumDTO = this.orderBiz.getUserProductOrderNum(userInfo.getBaseUserId());
        return APIObjResponse.success("查询成功", getOrderNumDTO);
    }

}
